package com.example.supermonkeysm.security.filter;

import com.example.supermonkeysm.security.entity.DetailsUser;
import com.example.supermonkeysm.security.utils.TokenUtils;
import com.example.supermonkeysm.utils.Result;
import com.example.supermonkeysm.utils.Utils;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;

import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Map;
import java.util.Objects;
import java.util.TreeMap;
import java.util.concurrent.TimeUnit;

/**
 * 表单登陆，登陆成功
 */
public class LoginSuccessHandler implements AuthenticationSuccessHandler {
    private final TokenUtils tokenUtils;
    private final RedisTemplate<String, Object> redisTemplate;
    public LoginSuccessHandler(TokenUtils tokenUtils, RedisTemplate<String, Object> redisTemplate) {
        this.tokenUtils = tokenUtils;
        this.redisTemplate = redisTemplate;
    }

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        DetailsUser userSecurity = (DetailsUser) authentication.getPrincipal();
        //使用redisDB为1
        RedisTemplate<String, Object> redisTemplate1 = Utils.setDataBase(redisTemplate, 1);
        //是否自动登录
        if (Objects.equals(request.getParameter("rememberMe"), "true")) {
            //生成1天内免登录的token
            String token = tokenUtils.createToken(userSecurity.getId(), userSecurity.getUsername(), 86400000);
            //存入redis
            Map<String, Object> redisMap = new TreeMap<>();
            redisMap.put("permissions", userSecurity.getPermissionList());
            redisMap.put("menus", userSecurity.getMenus());
            redisMap.put("token", token);
            redisTemplate1.opsForHash().putAll(userSecurity.getUsername(), redisMap);
            //设置redis过期时间
            redisTemplate1.expire(userSecurity.getUsername(), 86400000, TimeUnit.MILLISECONDS);
            //返回前端json
            Map<String, Object> map = new TreeMap<>();
            map.put("code", HttpServletResponse.SC_OK);
            map.put("token", token);
            map.put("msg", "登录成功,1天内免登录!");
            Result.customResult(response, map);
        } else {
            //生成规定时间的token
            String token = tokenUtils.createToken(userSecurity.getId(), userSecurity.getUsername(), tokenUtils.getTimeout());
            //存入redis
            Map<String, Object> redisMap = new TreeMap<>();
            redisMap.put("permissions", userSecurity.getPermissionList());
            redisMap.put("menus", userSecurity.getMenus());
            redisMap.put("token", token);
            redisTemplate1.opsForHash().putAll(userSecurity.getUsername(), redisMap);
            //设置redis过期时间
            redisTemplate1.expire(userSecurity.getUsername(), tokenUtils.getTimeout(), TimeUnit.MILLISECONDS);
            //返回前端json
            Map<String, Object> map = new TreeMap<>();
            map.put("code", HttpServletResponse.SC_OK);
            map.put("token", token);
            //毫秒转换为小时
            BigDecimal millisecond = new BigDecimal(tokenUtils.getTimeout() + "");
            //3600000 毫秒=1 时
            BigDecimal bigDecimal = new BigDecimal("3600000");
            /* 毫秒/小时 保留2位小数 四舍五入 */
            BigDecimal hour = millisecond.divide(bigDecimal, 2, RoundingMode.HALF_UP);
            map.put("msg", "登录成功," + hour + "小时后登录过期!");
            Result.customResult(response, map);
        }
    }
}
